#ifndef UNITY_SHADER_DT_URP_VFX_UTIL_INCLUDED
#define UNITY_SHADER_DT_URP_VFX_UTIL_INCLUDED


    // ==================================================================
    //
    // 全局特效的宏的开关
    //
    // ==================================================================
    //开启后，会执行自定义的较高消耗算法
    // #define DT_USE_CUSTOM_TIME
    
    // #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
    
    // #define DT_FNL_USE_POW
    // ==================================================================
    //
    // 引用的文件
    //
    // ==================================================================   
    #include "DTURPUtil.hlsl" //_with_pragmas

    // #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" 
    // #pragma multi_compile_instancing 
    // #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl" 
    


    
    // ==================================================================
    //
    // 结构体
    //
    // ==================================================================
    struct appdata_DT_VFX
    {
        DTfloat4 vertex         : POSITION;
        DTfloat4 vertexColor    : COLOR;
        float4 normal         : NORMAL;
        DTfloat4 texcoord0      : TEXCOORD0; // xy:UV;zw:particleCustom1.xy      |        
        DTfloat4 texcoord1      : TEXCOORD1; // xy:particleCustom1.zw zw:particleCustom2.xy  |
        DTfloat4 texcoord2      : TEXCOORD2; // xy:particleCustom2.zw   |        
        UNITY_VERTEX_INPUT_INSTANCE_ID
    };
    struct v2f_DT_VFX
    {
        DTfloat4 vertex         : SV_POSITION;//
        DTfloat4 vertexColor    : COLOR;      //
        DTfloat4 uv             : TEXCOORD0;  //#0 xy:uv0,zw:屏幕uv
        float4 positionWS       : TEXCOORD1;  //#1 xyz:positionWS,w:ScreenPos.w
        float4 normalWS         : TEXCOORD2;  //#2 xyz:normalWS,w:0
        DTfloat4 Custom1    : TEXCOORD3;  //#3 xy:主色uv,z:溶解
        DTfloat4 Custom2    : TEXCOORD4;  //#4 xy：遮罩uv,zw:溶解uv
        // DTfloat4 ####    : TEXCOORD5;  //#5
        // DTfloat4 ####    : TEXCOORD6;  //#6
        #ifdef UNITY_UI_CLIP_RECT
            DTfloat4 mask : TEXCOORD7;
        #endif
        UNITY_VERTEX_INPUT_INSTANCE_ID
        UNITY_VERTEX_OUTPUT_STEREO
    };    
    // ==================================================================
    //
    // 顶点函数
    //
    // ==================================================================
    v2f_DT_VFX DT_VFXvert(appdata_DT_VFX v)
    {
        //-------------------------------------------------------------
        //初始化
        //-------------------------------------------------------------
        v2f_DT_VFX o = (v2f_DT_VFX)0;
        UNITY_SETUP_INSTANCE_ID(v);
        UNITY_TRANSFER_INSTANCE_ID(v, o);
        UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
        //-------------------------------------------------------------
        //计算
        //-------------------------------------------------------------
        const DTfloat3 posOS =  v.vertex.xyz; 
        DTfloat3 posWS = TransformObjectToWorld(posOS);
        DTfloat4 posHCS = TransformWorldToHClip(posWS); 
        const DTfloat3 normalWS = TransformObjectToWorldNormal(v.normal.xyz, true); 
        const DTfloat4 screentmp = ComputeScreenPos(posHCS); 
        const DTfloat2 screenUV = screentmp.xy / DT_SAFE_VALUE(screentmp.w);//
        const DTfloat4 fullUV = DTfloat4(v.texcoord0.xy, screenUV.xy);

        #ifdef UNITY_UI_CLIP_RECT
            float2 pixelSize = posHCS.w;
            pixelSize /= float2(1.0, 1.0) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
            float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
            float2 maskUV = (posOS.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
            float4 clipmask = float4(posOS.xy * 2.0 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * float2(_UIMaskSoftnessX, _UIMaskSoftnessY) + abs(pixelSize.xy)));
        #endif        
        //-------------------------------------------------------------
        //赋值开始
        //-------------------------------------------------------------
        o.vertex = posHCS;
        o.uv = fullUV;
        o.vertexColor = v.vertexColor; 
        o.positionWS = float4(posWS.x,posWS.y,posWS.z,screentmp.w);
        o.normalWS = float4(normalWS.x,normalWS.y,normalWS.z, 0);
        o.Custom1 = DTfloat4(v.texcoord0.zw,v.texcoord1.xy);
        o.Custom2 = DTfloat4(v.texcoord1.zw,v.texcoord2.xy); 
        #ifdef UNITY_UI_CLIP_RECT
            o.mask = clipmask;
        #endif
        return o;     
    }
    // ====================================================================================================================================
    //
    //
    //
    //  
    //
    //
    //
    // ====================================================================================================================================    
    struct FragTexUVDate
    {
        //开关组
        DTfloat IsSceneUV;
        DTfloat IsPolar;
        DTfloat IsClampX;
        DTfloat IsClampY;
        DTfloat IsFilpX;
        DTfloat IsFilpY;

        DTfloat IsDistort;
        DTfloat IsFrame;
        DTfloat Uvalue;
        DTfloat Vvalue; 
        DTfloat RotValue;

        DTfloat3 MatrM0;
        DTfloat3 MatrM1;
        DTfloat2 UVDistValue;
    };
   

    DTfloat2 GetFragTexUV(v2f_DT_VFX i, inout FragTexUVDate d, DTfloat2 _distValue )
    {
        /*
            变换过程优先级:
            1.模型UV/屏幕UV
            4.矩阵变换(一次性的，旋转，缩放（翻转），偏移)
            2.极坐标
            3.扰动UV
            5.动态偏移UV
            6.clamp限定0-1的UV
        */
        DTfloat2 uv = i.uv.xy;
        //屏幕UV
        uv =  DT_SAFE_IF(d.IsSceneUV, i.uv.zw, i.uv.xy);
        //设置性质的UV矩阵，负责旋转，反相，缩放，偏移。性能高可读性差,不要用来做动画。
        //矩阵是由C#层在面板里计算好的。
        #ifdef NoRotationMatrix
            uv = dt_SetMatrixUVWork(uv, -d.RotValue, d.MatrM0, d.MatrM1);
        #else
            uv = dt_SetMatrixUVWork(uv, d.MatrM0, d.MatrM1);
        #endif
        // 
        //极坐标 
        uv = DT_SAFE_IF(d.IsPolar, GetPolarCoord(uv,DTfloat2(0.5,0.5)), uv);
        //扰动UV
        uv = uv + ( uv * _distValue * d.IsDistort * (-1 * d.UVDistValue));
        //偏移UV 
        uv.x += -1.0 * d.Uvalue;
        uv.y += -1.0 * d.Vvalue;  
        uv.x = DT_SAFE_IF(d.IsClampX,clamp(uv.x,0.0,1.0),uv.x);
        uv.y = DT_SAFE_IF(d.IsClampY,clamp(uv.y,0.0,1.0),uv.y);         
        return uv;
    }
    // DTfloat2 GetFragTexUV_II(v2f_DT_VFX i, inout FragTexUVDate d, DTfloat2 _distValue)
    // {
    //     /*
    //         变换过程优先级:
    //         1.模型UV/屏幕UV
    //         4.矩阵变换(一次性的，旋转，缩放（翻转），偏移)
    //         2.极坐标
    //         3.扰动UV           
    //         5.动态偏移UV
    //         6.clamp限定0-1的UV
    //     */
    //     DTfloat2 uv = i.uv.xy;
    //     //屏幕UV
    //     uv =  DT_SAFE_IF(d.IsSceneUV, i.uv.zw, i.uv.xy);
    //     //设置性质的UV矩阵，负责旋转，反相，缩放，偏移。性能高可读性差,不要用来做动画。
    //     //矩阵是由C#层在面板里计算好的。
    //     uv = dt_SetMatrixUVWork(uv, d.MatrM0, d.MatrM1);
    //     //极坐标
    //     uv = DT_SAFE_IF(d.IsPolar, GetPolarCoord(uv,float2(0.5,0.5)), uv);
    //     //扰动UV
    //     uv = uv + ( uv * _distValue * d.IsDistort);
    //     //偏移UV
    //     const DTfloat offx[3] = {d.USpeed,dt_AutoValue(d.USpeed),d.particleCustomU};
    //     const DTfloat offy[3] = {d.VSpeed,dt_AutoValue(d.VSpeed),d.particleCustomV};
    //     uv.x += -1.0 * offx[d.IsUVModeU];
    //     uv.y += -1.0 * offy[d.IsUVModeV];
    //     uv.x = DT_SAFE_IF(d.IsClampX,clamp(uv.x,0.0,1.0),uv.x);
    //     uv.y = DT_SAFE_IF(d.IsClampY,clamp(uv.y,0.0,1.0),uv.y);
    //     // d.NoOffsetUV.x = DT_SAFE_IF(IsClampX,clamp(d.NoOffsetUV.x,0.0,1.0),d.NoOffsetUV.x);
    //     // d.NoOffsetUV.y = DT_SAFE_IF(IsClampY,clamp(d.NoOffsetUV.y,0.0,1.0),d.NoOffsetUV.y); 
    //     return uv;
    // } 
    // ==================================================================
    //
    // 颜色贴图的函数
    //
    // ==================================================================
    struct ColorTexData
    {
        bool IsOn;
        DTfloat Intensity;
        DTfloat4 o_Color;
    };

    
    // ==================================================================
    //
    // 扰动值函数
    //
    // ==================================================================
    struct DistortTexData
    {
        bool IsOn;
        DTfloat2 DistFactor;
        DTfloat2 o_DistColor; //从贴图输入的值
    };

    DTfloat2 DistortFun(DistortTexData d)
    {
        if(d.IsOn)
        {
            return d.o_DistColor * d.DistFactor;
        }
        return DTfloat2(0,0);
    }
    // ==================================================================
    //
    // 溶解函数
    //
    // ==================================================================
    struct DissolveTexData
    {
        DTfloat3 LineColor;
        bool IsOn;
        DTfloat o_InputColor; //从贴图输入的值
        DTfloat I_distValue;       
        DTfloat Value;
        DTfloat Soft;
        DTfloat Wide;
        DTfloat distUPow;
        int IsDistLine;
        int IsDiteMode;
    };
    DissolveTexData Set_DissolveData(v2f_DT_VFX i,DTfloat dissvalue,
        DTfloat enable,DTfloat distinValue,
        DTfloat4 LineColor,
        DTfloat4 datavalue,DTfloat _animvalue )
    {
        DissolveTexData outval = (DissolveTexData)0; 
        outval.LineColor = LineColor.rgb * LineColor.a;//线颜色
        outval.IsOn = enable > 0;
        outval.o_InputColor = dissvalue;
        outval.I_distValue = distinValue; 
        outval.distUPow = datavalue.z;  
        //开关组
        int sw = (int)datavalue.w ; //W        
        outval.IsDiteMode = sw >> 4 & 0x1; //W
        outval.IsDistLine = sw >> 5 & 0x1;
        int mode = sw >> 0 & 0xF;

        float dissvMode[] = {
            _animvalue,i.vertexColor.a, //dt_AutoValue(datavalue.x),
            i.Custom1.x,i.Custom1.y,i.Custom1.z,i.Custom1.w,
            i.Custom2.x,i.Custom2.y,i.Custom2.z,i.Custom2.w,
        };         
        outval.Value = dissvMode[mode % 10]; //溶解值
        outval.Soft = datavalue.x; //柔边
        outval.Wide = datavalue.y; //宽
        return outval;            
    }
    DTfloat2 Get_DissolveClip(DissolveTexData d)
    {
        DTfloat2 outval =  DTfloat2(1,0);
        if(d.IsOn)
        {            
            float s = clamp( d.Soft, 0.01, 0.99);
            DTfloat tmpvalue = saturate(d.o_InputColor) + 1.0; //把贴图色从0-1 => 1-2
            float InValue = dt_Ramp_float(d.Value, 0.0, 1.0, -d.Wide * d.IsDiteMode, 1.0);
            float dissv = InValue * (2.0 - s * d.IsDiteMode)  ; //递减颜色的溶解值
            DTfloat value0 = tmpvalue + dissv * -1.0;
            DTfloat softvalue = smoothstep(s, 1.0, value0 ); //基础溶解  
            DTfloat edge = 1;
            if(d.IsDiteMode > 0.0001)
            { 
                //Dite版本的溶解
                edge = smoothstep(s, 1.0 , value0 - d.Wide); 
                edge = d.Wide > 0.000001? edge : 1.0;
            } 
            else
            {  
                //URP-YoFi/VFX/Universal 版本的溶解
                float w = clamp( d.Wide, 0.01, 0.99); 
                edge = smoothstep( w - s * 0.5, w, softvalue );                
            }
            softvalue += softvalue * d.distUPow * d.IsDistLine * d.I_distValue;
            outval = DTfloat2(softvalue, 1.0 - edge); 
        }
        return saturate(outval);
    }

    // ==================================================================
    //
    // 菲涅尔函数
    //
    // ==================================================================
    struct FNLData
    { 
        bool IsOn;
        DTfloat4 FNL_Color; 
        DTfloat Scale;  
        DTfloat Power;  
        DTfloat Brightness;
        DTfloat SW;
    };


    DTfloat Get_FNLValue(v2f_DT_VFX i,FNLData d)
    {        
        if(d.IsOn)
        {            
            int sw = (int)d.SW;
            DTfloat ReFnl = 1.0 - (sw >> 5 & 0x1); //W
            DTfloat IsDiss = 1.0 - (sw >> 6 & 0x1); //W
            DTfloat IsDist = 1.0 - (sw >> 7 & 0x1); //W
            //正交时的FNL算法 
            DTfloat fnlvalue = 0;
            if(IsPerspectiveProjection())//当是透视相机时
            { 
                DTfloat3 worldViewDir = GetWorldSpaceNormalizeViewDir(i.positionWS.xyz);
                fnlvalue = dot(i.normalWS.xyz, worldViewDir);
            }
            else
            {
                DTfloat3 normalView = mul((DTfloat3x3)UNITY_MATRIX_V, i.normalWS.xyz).xyz;
                fnlvalue = clamp(normalView.z, -1.0, 1.0);
            }
            fnlvalue = saturate( abs(fnlvalue)); //abs(1.0 - fnlvalue) 没看懂的错误，如果输入的透视的，得到是-1-0-1， 1-后得到2-0-1。那这个结果是不可控的。
            //=============================================
            //使用开方函数对于最终效果和操作会比较友好，但对性能不友好。
            #ifdef DT_FNL_USE_POW
                fnlvalue = pow(fnlvalue, d.Power) * d.Scale;
            #else 
                //d.scale = 0<->2,
                fnlvalue = smoothstep(max(0, d.Scale - saturate(1.0 - d.Power)), d.Scale, fnlvalue);
            #endif
            
            fnlvalue = saturate(fnlvalue);
            fnlvalue = abs(ReFnl - fnlvalue);//反相 
            //=============================================
            return fnlvalue;
        }
        return 1;
    }
    DTfloat Get_FNLValue(v2f_DT_VFX i,FNLData d,DTfloat distvalue)
    { 
        if(d.IsOn)
        {            
            int sw = (int)d.SW;
            DTfloat ReFnl = 1.0 - (sw >> 5 & 0x1); //W
            DTfloat IsDiss = (sw >> 6 & 0x1); //W
            DTfloat IsDist = (sw >> 7 & 0x1); //W
            // DTfloat IsCustom = (sw >> 14 & 0x1); //W
            //正交时的FNL算法 
            DTfloat fnlvalue = 0;
            if(IsPerspectiveProjection())//当是透视相机时
            { 
                DTfloat3 worldViewDir = GetWorldSpaceNormalizeViewDir(i.positionWS.xyz);
                fnlvalue = dot(i.normalWS.xyz, worldViewDir);
            }
            else
            {
                DTfloat3 normalView = mul((DTfloat3x3)UNITY_MATRIX_V, i.normalWS.xyz).xyz;
                fnlvalue = clamp(normalView.z, -1.0, 1.0);
            }
            fnlvalue = saturate(1.0 - abs(fnlvalue));
            fnlvalue = fnlvalue + saturate(fnlvalue * distvalue * IsDist);
            //=============================================
            //使用开方函数对于最终效果和操作会比较友好，但对性能不友好。
            #ifdef DT_FNL_USE_POW
                fnlvalue = pow(fnlvalue, d.Power) * d.Scale;
            #else 
                //d.scale = 0<->2,
                fnlvalue = smoothstep(max(0, d.Scale - saturate(1.0 - d.Power)), d.Scale, fnlvalue);
            #endif 
            fnlvalue = saturate( abs((1.0 - ReFnl) - fnlvalue));
            return fnlvalue;
        }
        return 1;
    }
     

    DTfloat3 Get_FNLCol(DTfloat3 finalColor,DTfloat _fnlvalue, FNLData d)
    { 
        int mode =  (int)d.SW & 0x7;
        mode = mode % 2;
        DTfloat3 _fnlCol =  d.FNL_Color.rgb * d.FNL_Color.a * _fnlvalue * d.Brightness;
        DTfloat3 outfnlcolor = lerp(saturate(finalColor + _fnlCol), saturate(finalColor * _fnlCol), mode);
        // DTfloat3 outfnlcolor[2] = {
        //     saturate(finalColor + _fnlCol),
        //     saturate(finalColor * _fnlCol),
        //     // lerp(_fnlCol, finalColor ,d.Factor * (1.0 -_fnlvalue))
        // };
        return lerp(0, outfnlcolor , d.IsOn);
    }
    DTfloat Get_FNLAlp(DTfloat _fnlvalue, FNLData d)
    {         
        DTfloat IsUseAlpha = (int)d.SW >> 4 & 0x1;
        return lerp(1,_fnlvalue, IsUseAlpha);
    }
    // ==================================================================
    //
    // 地面裁切函数
    //
    // ==================================================================    
    struct GroundClipData
    {
        bool IsOn;
        float ClipBlur;
    };
    DTfloat Get_GroundClip(v2f_DT_VFX i,GroundClipData d)
    {
        if(d.IsOn){		
            return smoothstep(0, d.ClipBlur,i.positionWS.y);
        } 
        return 1.0; //与最终的颜色A通道相乘
    } 
    // ==================================================================
    //
    // 软粒子
    //
    // ==================================================================
    // #define _PARTICLE_DEPTH_SOFT_ON
    DTfloat Get_SoftParticle(v2f_DT_VFX i, DTfloat DepthFade)
    {
        // half softParticleFactor = 1.0;
        #ifdef _PARTICLE_DEPTH_SOFT_ON
            float depth = LinearEyeDepth(SampleSceneDepth(i.uv.zw), _ZBufferParams);
            float selfDepth = LinearEyeDepth(ndc.z, _ZBufferParams);
            float diffRamp = abs(depth - i.positionWS.w) / DepthFade;
            return saturate(diffRamp);
        #endif
        //用输出的值去乘最终的A通道。
        return 1.0;
    }
    // ==================================================================
    //
    // YF Bloom 函数
    //
    // ==================================================================
    struct BloomData
    {
        bool CBGlobalBool;
        bool CBLocalBanBool;
        DTfloat CBLocalThreshold;
        DTfloat CBLocalLight;
        DTfloat4 CBLocalColor;
    };

    DTfloat4 Get_BloomOut(DTfloat4 finalColor,BloomData d)
    {
        if(d.CBLocalBanBool){
            return saturate(finalColor);
        }
        if(d.CBGlobalBool){
            float cc = dot(finalColor.rgb, float3(0.2126729f,  0.7151522f, 0.0721750f)) ;
            half bcol = smoothstep(1.0 - max(0,d.CBLocalThreshold), 1.0 , cc);
            half3 nc =  bcol;
            half3 value = nc * d.CBLocalLight * d.CBLocalColor.rgb;
            value *= finalColor.rgb * finalColor.a;
            finalColor.rgb += value;
            return finalColor;
        }
        else{
            return saturate(finalColor);
        }
    }
    // ==================================================================
    //
    // Gpu Instancing
    //
    // ==================================================================
    // 目前项目还未对特效做Gpu Instancing
    // GPU Instancing
    // #ifdef DT_URP_MODE
    //     float4 _XXXXX;
    //     #ifdef UNITY_DOTS_INSTANCING_ENABLED
    //         UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)
    //             UNITY_DOTS_INSTANCED_PROP(float4, _XXXXX)
    //         UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)

    //         static float4 unity_DOTS_Sampled_XXXXX; 

    //         void SetupDOTSUnlitMaterialPropertyCaches()
    //         {
    //             unity_DOTS_Sampled_XXXXX   = UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT(float4, XXXXX); 
    //         }

    //         #undef UNITY_SETUP_DOTS_MATERIAL_PROPERTY_CACHES
    //         #define UNITY_SETUP_DOTS_MATERIAL_PROPERTY_CACHES() SetupDOTSUnlitMaterialPropertyCaches()

    //         #define _XXXXX          unity_DOTS_Sampled_XXXXX 
    //     #endif
    // #else //DT_URP_MODE
    //     #if defined(UNITY_INSTANCING_ENABLED)
    //     UNITY_INSTANCING_BUFFER_START(Props)
    //         UNITY_DEFINE_INSTANCED_PROP(float4, _XXXXX)
    //     UNITY_INSTANCING_BUFFER_END(Props)
    //     #define _XXXXX          UNITY_ACCESS_INSTANCED_PROP(float4, _XXXXX)
    // #endif //DT_URP_MODE
    // ====================================================================================================================================
    //
    // 颜色特效
    //
    // ====================================================================================================================================
    /*
    float FilterBlackWhite; 黑白 【0-1】
    float FilterInvert; 反相 【0-1】
    */
    // float4 _v;
    // 
    DTfloat3 Get_BlackWhiteCol(DTfloat3 _col )
    {  
        DTfloat gray = dot(_col.rgb, DTfloat3(0.2126729, 0.7151522, 0.0721750));
        float edge =  length(float2(ddx(gray), ddy(gray)))   ; 
        return   DTfloat3(edge, edge, edge) + _col;
    }
    // 置灰
    DTfloat3 Get_GrayCol(DTfloat3 _col)
    { 
        DTfloat gray = dot(_col.rgb, DTfloat3(0.2126729, 0.7151522, 0.0721750));
        return lerp(_col,DTfloat3(gray, gray, gray),1.0);
    }
    //反相
    DTfloat3 Get_InvCol(DTfloat3 _col, DTfloat t)
    {  
        return abs(DTfloat3(t,t,t) - _col.rgb); 
    }
    DTfloat4 TexStyleWork(DTfloat4 _col)
    {
        DTfloat3 col = _col.rgb;
        DTfloat cola = _col.a;

        #ifndef UNITY_COLORSPACE_GAMMA 
            const DTfloat3 INV_GAMMA = 1.0 / 2.2;
            col = pow(col,INV_GAMMA);
        #endif
        col = Get_BlackWhiteCol(col);

        #ifndef UNITY_COLORSPACE_GAMMA
            const DTfloat3 GAMMA = 2.2;
            col = pow(col,GAMMA);
        #endif
        return DTfloat4(col,cola);
    }

    // half4 frag(VertexOutput IN) : SV_Target
    // {
    //     UNITY_SETUP_INSTANCE_ID( IN );
    //     UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX( IN );

    //     float2 uv = IN.uv.xy;
    //     float4 customData1 = IN.customData1;
    //     float4 customData2 = IN.customData2;


    //     float4 color_vertex = IN.color;
    //     half3 ndc = IN.positionSS.xyz / (IN.positionSS.w  + Min_float());
    //     half2 screenUV = ndc.xy;
    //     float time = _Time.y;
        
    //     float2 uv_MainTex_speed = (float2(_MainTexUSpeed , _MainTexVSpeed));
    //     float2 uv_MainTex = (_MainTexScreenUV ? screenUV : uv);
    //     uv_MainTex = uv_MainTex;// * _MainTex_ST.xy + _MainTex_ST.zw;
    //         //使用极坐标
    //     float2 uv_MainTex_Center =  float2(0.5,0.5);// * _MainTex_ST.xy;
    //     UNITY_BRANCH
    //     if(_MainTexUsePolarCoord){PolarCoord(uv_MainTex,uv_MainTex_Center);}
        
    //     float2 uv_MainTex_offset = uv_MainTex  + frac(time * uv_MainTex_speed) + _CustomMainTex *customData1.xy;
    //     float2 uv_colorTex_speed = (float2(_ColorTexUSpeed , _ColorTexVSpeed));
    //     float2 uv_colorTex = (_ColorTexScreenUV ? screenUV : uv) * _ColorTex_ST.xy + _ColorTex_ST.zw;
    //     float2 uv_colorTex_offset = uv_colorTex + frac(time * uv_colorTex_speed);

    //     //扭曲相关
    // #ifdef _FDISTORTTEX_ON
    //     float2 uv_DistortTex_speed = (float2(_DistortTexUSpeed , _DistortTexVSpeed));
    //     float2 uv_DistortTex = uv * _DistortTex_ST.xy + _DistortTex_ST.zw;
        
    //     //使用极坐标
    //     float2 uv_DistortTex_Center =  float2(0.5,0.5) * _DistortTex_ST.xy;
    //     UNITY_BRANCH
    //     if(_DistortTexUsePolarCoord){PolarCoord(uv_DistortTex,uv_DistortTex_Center);}
        
    //         uv_DistortTex = frac( time * uv_DistortTex_speed) + uv_DistortTex;
    //     half4 distortTex = 	tex2D(_DistortTex,uv_DistortTex);


    //     half2 distortValue = lerp(distortTex.a,distortTex.r,_DistortTexAR) * half2( _DistortFactorU,_DistortFactorV);
    //     uv_MainTex += distortValue * _DistortMainTex;	
    //     uv_MainTex_offset = uv_MainTex + frac(time * uv_MainTex_speed) + _CustomMainTex *customData1.xy ;
    // #endif


    //     half4 mainTex = tex2D(_MainTex,uv_MainTex_offset);
    //     half4 colorTex = tex2D(_ColorTex,uv_colorTex_offset);
    //     colorTex *= _ColorTexTint;
    //     colorTex.rgb *= lerp(1, mainTex.rgb,_ColorTexUseMainColorFactor);

    //     if(_ColorTexAddMode)
    //     {
    //         mainTex.rgb += colorTex.rgb * mainTex.rgb * colorTex.a * _ColorTexIntensity;
    //     }
    //     else
    //     {
    //         mainTex.rgb = lerp(mainTex.rgb, mainTex.rgb * colorTex.rgb,_ColorTexIntensity);
    //     }

        
    //     mainTex.a = lerp(mainTex.a , mainTex.r,_MainTexAR);
    //     half4 mainColor =  _MainColor;
    //     half4 mainColor_final = ( mainColor * mainTex  * color_vertex);


    //     //half dissolveAlpha_final = 1.0;
    //     half4 mainColor_final_useDissolve = mainColor_final;

    //     //溶解相关
    // #ifdef _FDISSOLVETEX_ON
    //     half2 uv_DissolveTex_speed = (half2(_DissolveTexUSpeed , _DissolveTexVSpeed));
    //     half2 uv_DissolveTex = uv * _DissolveTex_ST.xy + _DissolveTex_ST.zw;

    //     //使用极坐标
    //     float2 uv_DissolveTex_Center =  float2(0.5,0.5) * _DissolveTex_ST.xy;
    //     UNITY_BRANCH
    //     if(_DissolveTexUsePolarCoord){PolarCoord(uv_DissolveTex,uv_DissolveTex_Center);}
        
    //     #ifdef _FDISTORTTEX_ON
    //         uv_DissolveTex += distortValue * _DistortDissolveTex;
    //     #endif
    //     uv_DissolveTex = frac( time * uv_DissolveTex_speed) + uv_DissolveTex +  _CustomDissolve * customData2.zw ;
        
        
    //     half4 dissolveTex = tex2D(_DissolveTex,uv_DissolveTex);
    //     half dissolveTex_value = lerp(dissolveTex.a,dissolveTex.r,_DissolveTexAR);
    //     dissolveTex_value = _DissolveValueFlip ? 1 - dissolveTex_value : dissolveTex_value;
    //     //half dissolveFactor = lerp(_DissolveFactor,customData1.z, _CustomDissolve);
    //     half dissolveFactor = _CustomDissolve ? customData1.z : _DissolveFactor;

    //     //溶解算法
    //     float rampValue = 1;
    //     Unity_Remap_float(dissolveTex_value,float2(0,1 ),float2(1,2), rampValue);
    //     float value =  rampValue - dissolveFactor*2;
    //     float softvalue = smoothstep(_DissolveSoft*0.5,0.5,value);
    //     float edge = smoothstep(_DissolveWide-_DissolveSoft*0.5,_DissolveWide,softvalue);

    //     mainColor_final_useDissolve.rgb = lerp( _DissolveColor.rgb,mainColor_final_useDissolve.rgb,edge);
    //     mainColor_final_useDissolve.a *= softvalue;
        
    // #endif

    //     float4 color_final =  mainColor_final_useDissolve;

    //     half3 fresnel_color = half3(0,0,0);
    //     half fresnel_Alpha = 1.0;
    // #ifdef _FFNL_ON
    //     float3 worldNormal = IN.normalWS.xyz;
    //     float3 WorldPosition = IN.positionWS;
    //     half fresnelNdotV279 = 0;
    //     if(unity_OrthoParams.w>0)//当是正交相机时
    //     {
    //         float3 normalView = mul((float3x3)UNITY_MATRIX_V, worldNormal).xyz;
    //         fresnelNdotV279 = normalView.z;
    //     }
    //     else
    //     {
    //         float3 worldViewDir = ( _WorldSpaceCameraPos.xyz - WorldPosition );
    //         worldViewDir = normalize(worldViewDir);
    //         fresnelNdotV279 = dot( worldNormal, worldViewDir );
    //     }

        
    //     half refnl = _ReFnl;
    //     half fresnelNode279 = ( 0.0 + _FnlScale * pow(abs(1.0 - fresnelNdotV279), _FnlPower));
    //     half temp_output_283_0 = saturate( fresnelNode279 );
    //     half4 FnlMainColor286 = ( _FnlColor * temp_output_283_0 * _FnlColor.a );
    //     half ReFnlAlpha318 = ( 1.0 - temp_output_283_0 );
        
    //     FnlMainColor286 = lerp(FnlMainColor286,FnlMainColor286*color_final,_FnlMainColorFactor);
        
    //     fresnel_color = FnlMainColor286 * (1-refnl);
    //     fresnel_Alpha = lerp(1.0,ReFnlAlpha318,refnl);

    // #endif

    //     color_final.rgb  += fresnel_color.rgb;
    //     color_final.a *= fresnel_Alpha;

    //     // 遮罩相关
    //     half mask_final = 1.0;
    // #ifdef _FMASKTEX_ON

    //     half2 uv_MaskTex_speed = (half2(_MaskTexUSpeed , _MaskTexVSpeed));
    //     half2 uv_MaskTex = uv * _MaskTex_ST.xy + _MaskTex_ST.zw;
    //     half2 uv_MaskTex_Distort = uv_MaskTex;

    //     // 极坐标 Polar Coord
    //     half2 uv_PolarMask = uv_MaskTex_Distort - _MaskTex_ST.xy * 0.5;

    //     #ifdef _FDISTORTTEX_ON
    //         uv_MaskTex_Distort += distortValue * _DistortMaskTex;
    //     #endif
    //     half2 uv_MaskTex_Temp = uv_MaskTex_Distort + frac(time * uv_MaskTex_speed) + _CustomMaskTex *customData2.xy ;
            
    //     half4 maskTex = tex2D(_MaskTex,uv_MaskTex_Temp);
    //     mask_final = lerp(maskTex.a, maskTex.r, _MaskTexAR);
    //     mask_final = saturate(pow( clamp(mask_final + _MaskValueOffset,0,1), _MaskValuePower));


    // #endif




    //     float alpha_final = color_final.a * mask_final;


    //     /// 软粒子
    //     half softParticleFactor = 1.0;
    // #ifdef _FDEPTH_ON
    //     float depth = LinearEyeDepth(SampleSceneDepth(screenUV.xy), _ZBufferParams);
    //     float selfDepth = LinearEyeDepth(ndc.z, _ZBufferParams);
    //     float diffRamp = abs(depth - IN.positionSS.w) / _DepthFade;
    //     softParticleFactor = saturate(diffRamp);
    // #endif
    //     alpha_final *= softParticleFactor;
        
    //     // color_final.rgb = InvTonemap_ACES(color_final.rgb);
    //     half3 Color = lerp(color_final.rgb,color_final.rgb * alpha_final,_BlendMode);
    //     half Alpha = alpha_final;
        
    //     half4 finalColor = half4(Color, Alpha);
        
    // #ifdef UNITY_UI_CLIP_RECT
    //     half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(IN.mask.xy)) * IN.mask.zw);
    //     finalColor.a *= m.x * m.y;
    // #endif
        
    //     clip(finalColor.a - 0.000001);
        

    //     return finalColor;
    // } 
#endif // UNITY_SHADER_DT_URP_VFX_UTIL_INCLUDED
